//
//  UserAccountVieModel.swift
//  weibo
//
//  Created by 吴婷婷 on 15/11/7.
//  Copyright © 2015年 wutingting. All rights reserved.
//

import Foundation

//MARK: - 用户账户视图模型
class UserAccountViewModel {
    ///单例
    static let sharedUserAccount = UserAccountViewModel()
    ///用户账户
    var account: UserAccount?
    ///有效地Token
    var accessToken: String? {
        
        if !isExpired {
            return account?.access_token
        }
        return nil
    }
    //登陆标记
    var userLogon: Bool {
        return account?.access_token != nil && !isExpired
    }
    //用户头像路径
    var avatarURL: NSURL {
        return NSURL(string: account?.avatar_large ?? "")!
    }
    ///归档保存路径
    private var accountPath: String {
        var path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last!
        
        path = (path as NSString).stringByAppendingPathComponent("account.plist")
        
        return path
    }
    
    ///判断账户是否过期
    private var isExpired: Bool {
        
        //        account = nil
        
        if account?.expiresDate?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
            return false
        }
        
        return true
    }
    
    ///构造函数
    private init() {
        
        account = NSKeyedUnarchiver.unarchiveObjectWithFile(accountPath) as? UserAccount
        
        if isExpired == true {
            print("已经过期")
            
            account = nil
        }
        
        
    }
    
}

//MARK: - 用户账户相关网络方法
extension UserAccountViewModel {
 ///加载Token
    func loadAccessToken(code: String, finished: (isSucceed: Bool) -> ()) {
        
        NetworkTools.sharedTools.loadAccessToken(code) { (result, error) -> () in
            if (error != nil) {
                print("出错了")
                finished(isSucceed: false)
                return
            }
            
            self.account = UserAccount(dict: result as! [String: AnyObject])
            
            self.loadUserInfo(self.account!,finished: finished)
            
        }
    }
    
    ///加载用户信息
    private func loadUserInfo(account: UserAccount, finished: (isSucceed: Bool) -> ()){
        
        NetworkTools.sharedTools.loadUserInfo(account.uid!) { (result, error) -> () in
            if error != nil {
                print("出错了")
                finished(isSucceed: false)
                return
            }

            
            guard let dict = result as? [String: AnyObject] else {
                print("格式错误")
                finished(isSucceed: false)
                return
            }
            
            account.screen_name = dict["screen_name"] as? String
            account.avatar_large = dict["avatar_large"] as? String
            
//            account.saveUserAccount()
            NSKeyedArchiver.archiveRootObject(account, toFile: self.accountPath)
            print(self.accountPath)
            
            finished(isSucceed: true)
            
        }
    }
}












